package com.cos.shop.dao.impl;

import com.cos.shop.common.Const;
import com.cos.shop.dao.UserDao;
import com.cos.shop.entity.User;
import com.cos.shop.util.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author: COS
 * @time: 2022/2/23 14:37
 * @description:
 */
public class UserDaoImpl implements UserDao {
    private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());

    @Override
    public int activeUser(int uid) throws SQLException {
        return qr.update("update user set ustatus=? where uid=?",
                Const.IS_ACTIVE,uid);
    }

    @Override
    public User getUserByCodeAndEmail(String email, String code) throws SQLException {
        return qr.query("select * from user where uemail=? and ucode=?", new BeanHandler<>(User.class),email,code);
    }

    @Override
    public int addUser(User u) throws SQLException {
        return qr.update("insert into user values (default,?,?,?,?,?,?,?)",
                u.getUname(),u.getUpassword(),u.getUemail(),u.getUsex(),u.getUstatus(),u.getUcode(),u.getUrole());
    }

    @Override
    public User checkUserName(String username) throws SQLException {
        return qr.query("select * from user where uname = ?",new BeanHandler<>(User.class),username);
    }
}
